package cn.edu.fzu.homemaking.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.edu.fzu.homemaking.common.PageVO;
import cn.edu.fzu.homemaking.domain.dto.SysCompanyDTO;
import cn.edu.fzu.homemaking.domain.entity.SysCompany;
import cn.edu.fzu.homemaking.domain.enums.StatusEnum;
import cn.edu.fzu.homemaking.service.SysCompanyService;
import cn.edu.fzu.homemaking.web.Result;
import io.swagger.annotations.*;

/**
 * @author yifan
 * @date 2023/4/4
 */
@Api(tags = "sys-家政公司管理")
@RestController
@RequestMapping("/api/sys")
public class SysCompanyController {

    @Resource
    SysCompanyService sysCompanyService;

    @ApiOperation(value = "获取公司列表", httpMethod = "GET")
    @ApiResponse(code = 200, message = "", response = Result.class)
    @ApiImplicitParams({ @ApiImplicitParam(name = "pageNum", required = true, value = "页码"),
            @ApiImplicitParam(name = "pageSize", required = true, value = "页面大小"),
            @ApiImplicitParam(name = "creditCode", required = false, value = "统一信用代码"),
            @ApiImplicitParam(name = "name", required = false, value = "名字"),
            @ApiImplicitParam(name = "legalPerson", required = false, value = "法人"),
            @ApiImplicitParam(name = "businessLicense", required = false, value = "营业执照"),
            @ApiImplicitParam(name = "contact", required = false, value = "联系人"),
            @ApiImplicitParam(name = "provinceId", required = false, value = "省id"),
            @ApiImplicitParam(name = "cityId", required = false, value = "市id"),
            @ApiImplicitParam(name = "areaId", required = false, value = "地区id"),
            @ApiImplicitParam(name = "labelId", required = false, value = "标签id"),
            @ApiImplicitParam(name = "phone", required = false, value = "电话"),
            @ApiImplicitParam(name = "status", required = false, value = "-1-删除，0-正常，1-禁用") })
    @GetMapping("/company/list")
    public Result getCompanyList(String name, String creditCode, String legalPerson, String businessLicense,
                                 String contact, String phone, StatusEnum status, Integer pageNum, Integer pageSize,
                                 Long provinceId, Long cityId, Long areaId, Long labelId) {
        try (Page<SysCompany> page = PageHelper.startPage(pageNum, pageSize)) {
            List<SysCompany> companyList = sysCompanyService.list(name, creditCode, legalPerson, businessLicense,
                    contact, phone, status, areaId, cityId, provinceId, labelId);
            return new Result(new PageVO<>(page.getTotal(), companyList));
        }
    }


    @ApiOperation(value = "根据id获取公司信息", httpMethod = "GET")
    @ApiResponse(code = 200, message = "", response = Result.class)
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", required = true, value = "公司id") })
    @GetMapping("/company/getById")
    public Result getCompanyById(Long id) {
        SysCompanyDTO company = sysCompanyService.getById(id);
        return new Result(company);
    }


    @ApiOperation(value = "录入公司", httpMethod = "POST")
    @ApiResponse(code = 200, message = "", response = Result.class)
    @ApiImplicitParams({ @ApiImplicitParam(name = "name", required = true, value = "名字"),
            @ApiImplicitParam(name = "creditCode", required = true, value = "统一信用代码"),
            @ApiImplicitParam(name = "legalPerson", required = true, value = "法人"),
            @ApiImplicitParam(name = "businessLicense", required = true, value = "营业执照"),
            @ApiImplicitParam(name = "contact", required = true, value = "联系人"),
            @ApiImplicitParam(name = "phone", required = true, value = "电话"),
            @ApiImplicitParam(name = "areaId", required = false, value = "地区id"),
            @ApiImplicitParam(name = "address", required = false, value = "详细地址") })
    @PostMapping("/auth/company/add")
    public Result addCompany(String name, String creditCode, String legalPerson, String businessLicense, String contact,
                             String phone, Long areaId, String address) {
        sysCompanyService.addCompany(name, creditCode, legalPerson, businessLicense, contact, phone, areaId, address);
        return Result.SUCCESS;
    }


    @ApiOperation(value = "更新公司信息", httpMethod = "POST")
    @ApiResponse(code = 200, message = "", response = Result.class)
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", required = true, value = "公司id"),
            @ApiImplicitParam(name = "name", required = false, value = "名字"),
            @ApiImplicitParam(name = "creditCode", required = false, value = "统一信用代码"),
            @ApiImplicitParam(name = "legalPerson", required = false, value = "法人"),
            @ApiImplicitParam(name = "businessLicense", required = false, value = "营业执照"),
            @ApiImplicitParam(name = "contact", required = false, value = "联系人"),
            @ApiImplicitParam(name = "phone", required = false, value = "电话"),
            @ApiImplicitParam(name = "status", required = false, value = "-1-删除，0-正常，1-禁用"),
            @ApiImplicitParam(name = "areaId", required = false, value = "地区id"),
            @ApiImplicitParam(name = "address", required = false, value = "详细地址") })
    @PostMapping("/auth/company/update")
    public Result updateCompany(Long id, String name, String creditCode, String legalPerson, String businessLicense,
                                String contact, String phone, StatusEnum status, Long areaId, String address) {
        sysCompanyService.updateCompany(id, name, creditCode, legalPerson, businessLicense, contact, phone, status,
                areaId, address);
        return Result.SUCCESS;
    }


    @ApiOperation(value = "分配公司标签", httpMethod = "POST")
    @ApiResponse(code = 200, message = "", response = Result.class)
    @PostMapping("/auth/company/label/add")
    public Result assignLabel(Long companyId, Long labelId) {
        sysCompanyService.assignLabel(companyId, labelId);
        return Result.SUCCESS;
    }


    @ApiOperation(value = "移除公司标签")
    @ApiResponse(code = 200, message = "", response = Result.class)
    @PostMapping("/auth/company/label/remove")
    public Result removeLabel(Long companyId, Long labelId) {
        sysCompanyService.removeLabel(companyId, labelId);
        return Result.SUCCESS;
    }
}
